package com.xjh.him.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xjh.him.common.LoginUser;
import com.xjh.him.dao.FriendMapper;
import com.xjh.him.dao.UserMapper;
import com.xjh.him.domain.FriendDO;
import com.xjh.him.domain.UserDO;
import com.xjh.him.dto.friend.FriendDTO;
import com.xjh.him.dto.friend.FriendQueryResultDTO;
import com.xjh.him.dto.user.UserDTO;
import com.xjh.him.util.PinyinUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xjh.tool.Checks;
import org.xjh.tool.common.ErrorMsg;
import org.xjh.tool.dto.ResultDTO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class FriendServiceImpl implements IFriendService {

    @Autowired
    private FriendMapper friendMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResultDTO<Map<String, List<FriendDTO>>> listFriend() {
        UserDO loginUser = LoginUser.getLoginUser();
        List<FriendDO> friendDOList = friendMapper.queryFriend(loginUser.getId());
        if (Checks.isEmpty(friendDOList)) {
            return ResultDTO.getSuccessResult(new HashMap<>());
        }
        List<Long> userIdList = friendDOList.stream().map(item -> {
            if (item.getFirstId().equals(loginUser.getId())) {
                return item.getSecondId();
            } else {
                return item.getFirstId();
            }
        }).collect(Collectors.toList());
        List<UserDO> userDOList = userMapper.selectBatchIds(userIdList);
        Map<String, List<FriendDTO>> map = new HashMap<>();
        for (UserDO userDO : userDOList) {
            String firstChar = PinyinUtil.getFirstChar(userDO.getNickname());
            FriendDTO friendDTO = new FriendDTO();
            BeanUtils.copyProperties(userDO, friendDTO);
            List<FriendDTO> friendDTOList = map.get(firstChar);
            if (friendDTOList != null) {
                friendDTOList.add(friendDTO);
            }else {
                friendDTOList = new ArrayList<>();
                friendDTOList.add(friendDTO);
                map.put(firstChar, friendDTOList);
            }
        }
        return ResultDTO.getSuccessResult(map);
    }

    @Override
    public ResultDTO<FriendQueryResultDTO> verifyFriend(String phone) {
        if (Checks.isEmpty(phone)) {
            return ResultDTO.getFailureResult(ErrorMsg.ERR_PARAMETER_IS_NULL);
        }
        QueryWrapper<UserDO> queryUserQW = new QueryWrapper<>();
        queryUserQW.eq("phone", phone);
        UserDO userDO = userMapper.selectOne(queryUserQW);
        if (userDO == null) {
            return ResultDTO.getFailureResult("手机号不存在");
        }
        UserDO loginUser = LoginUser.getLoginUser();
        QueryWrapper<FriendDO> countFriend = new QueryWrapper<>();
        countFriend.eq("first_id", Math.min(loginUser.getId(), userDO.getId()));
        countFriend.eq("second_id", Math.max(loginUser.getId(), userDO.getId()));
        Integer count = friendMapper.selectCount(countFriend);
        FriendQueryResultDTO resultDTO = new FriendQueryResultDTO();
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(userDO, userDTO);
        resultDTO.setUserDTO(userDTO);
        resultDTO.setIsFriend(count == 1);
        return ResultDTO.getSuccessResult(resultDTO);
    }
}
